package com.szpxt.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.activiti.engine.RuntimeService;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.szpxt.model.User;
import com.szpxt.service.UserService;

@Component @Scope("prototype")
public class ApplyAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map<String, Object> sessionMap;
	
	private UserService userService;
	private RuntimeService runtimeService;
	
	public void logout() throws IOException {
		sessionMap.clear();
		
		JSONObject jo = new JSONObject();
		jo.put(SUCCESS, true);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void getAnalysts() throws IOException {
		JSONObject jo = new JSONObject();
		
		List<User> listUser = userService.getAnalysts();
		JSONArray array = new JSONArray();
		for(User element: listUser) {
			JSONObject object = new JSONObject();
			object.put("id", element.getId());
			object.put("name", element.getName());
			
			array.add(object);
		}
		
		jo.put(SUCCESS, true);
		jo.put("data", array.toString());
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void create() throws IOException {
		String name = request.getParameter("name");
		String company = request.getParameter("company");
		String mobile = request.getParameter("mobile");
		String eMail = request.getParameter("eMail");
		double amount = Double.parseDouble(request.getParameter("amount"));
		
		String userId = request.getParameter("userId");
		String activitiId = "";
		if(StringUtils.isBlank(userId)) {
			activitiId = userService.getRandomAnalyst().getActivitiId();
		}
		else {
			activitiId = userService.get(Long.parseLong(userId)).getActivitiId();
		}
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		int serialNumber = 0;
		while(serialNumber >= 10000000 || serialNumber < 1000000) {
			serialNumber = (int)(Math.random() * 10000000);
		}		
		String strSerialNumber = simpleDateFormat.format(new Date()) + serialNumber;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("company", company);
		map.put("mobile", mobile);
		map.put("eMail", eMail);
		map.put("amount", amount);
		map.put("activitiId", activitiId);
		map.put("serialNumber", strSerialNumber); 
		
		runtimeService.startProcessInstanceByKey("financing", map);
		
		JSONObject jo = new JSONObject();
		jo.put(SUCCESS, true);
		jo.put("serialNumber", strSerialNumber);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	//获取和设置方法
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public RuntimeService getRuntimeService() {
		return runtimeService;
	}

	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}
	
}